<template>
    <view class="page-content p-b-30" style="background:#F6F6F6;">
        <view class="h277 width100 bg-cover p-t-50" :style="`background-image:url(${require('../../../static/img3/mywallet.png')});background-position:0 -30rpx;`">
            <view @click="goto('/pages/wallet/walletManager/index')" class="view flex-row align-center justify-between p-l-30 p-r-30 p-t-20">
                <view class="view flex-row align-center">
                    <image src="/static/token/DSX.png" class="w60 h60 m-r-10"/>
                    <view>
                        <view class="fz-20 c-fff bold">{{Object.keys(defaultWallet).length > 0 ? defaultWallet.name : '未创建钱包'}}</view>
                        <view class="fz-12 c-fff ">{{defaultWallet.address | $hiddenString(6,6)}}</view>
                    </view>
                </view>
                <view class="view flex-row align-center">
                    <view class="fz-12 c-fff m-r-7">切换钱包</view>
                    <uni-icons type="right" color="#fff" size="12"/>
                </view>
            </view>

			

            <view class="view flex-row align-center justify-center m-t-20">
                <view @click="goto('/pages/wallet/imporWallet/index')" class="view align-center justify-center m-r-60">
                    <image src="/static/img1/mywallet_icon_import wallet_default.png" class="w25 h25 m-b-10"/>
                    <view class="fz-12 c-fff">导入钱包</view>
                </view>

                <view @click="goto('/pages/wallet/createWallet/index')" class="view align-center justify-center m-r-60">
                    <image src="/static/img1/mywallet_icon_Create wallet_default.png" class="w25 h25 m-b-10"/>
                    <view class="fz-12 c-fff">创建钱包</view>
                </view>

                <view @click="toDeleteWallet" class="view align-center justify-center">
                    <image src="/static/img1/mywallet_icon_Delete wallet_default.png" class="w25 h25 m-b-10"/>
                    <view class="fz-12 c-fff">删除钱包</view>
                </view>
				<uni-popup ref="popup1" type="center" :mask-click="false">
					<view  class="popupCenter">
					<view class="center_title">提示</view>
					<view class="center_content">您正在删除钱包，删除后只能通过助记词和私钥重新导入，请在删除前务必确认已备份住几次或私钥</view>
					<view class="center_button">
						<view class="btn1" @click="toPrivateKeyTip(defaultWallet.type=='mnemonic' ? '1' : '2')" >{{defaultWallet.type=='mnemonic' ? '备份助记词' : '备份私钥' }}</view>
						<view class="btn2" @click="deleteWalletAndSetDefault">{{'我已备份，删除钱包'}}</view>
					</view>
					</view>
				</uni-popup>
            </view>
        </view>
        
		
		
			
        <view class="view align-center justify-center">
			<view class="mauto bg-fff p-t-20 p-b-20 p-l-20 p-r-20 mypack-list  m-b-12" style="width:calc(100vw - 150rpx);border-radius: 20px;margin-top:-170rpx;" >
					<view class="view flex-row m-b-15 h48 b-b-1">
						<view class="view  align-center flex-1" @click="yqmcode">
							<view class="m-b-7 fz-13">我的邀请码</view>
							<view class="c-a0a fz-12">{{ userInfo.yqmcode ? userInfo.yqmcode : '暂无邀请码' }}</view>
						</view>
						<view class="view align-center flex-1" @click="bindCode">
							<view class="m-b-7 fz-13">邀请人绑定</view>
							<view class="c-a0a fz-12">{{ userInfo.is_superior == 1 ? '已绑定':'未绑定'}}</view>
							<uni-popup ref="popup_bind" type="center" :mask-click="false">
								<view  class="popupCenter">
								<view class="center_title">{{'邀请人绑定'}}</view>
								<view class="center_content">{{'绑定邀请人后无法修改'}}</view>
								<view class="center_content">
									<uni-easyinput type="text" v-model="superiorCode" :placeholder="'请输入邀请人的邀请码'"></uni-easyinput>
								</view>
								<view class="center_button">
									<view class="btn1" @click="closeBind()">{{'取消'}}</view>
									<view class="btn2" @click="sendBind()">{{'确定提交'}}</view>
								</view>
								</view>
							</uni-popup>
						</view>
						<view class="view  align-center flex-1" @click="notOpen">
							<view class="m-b-7 fz-13">邀请人数</view>
							<view class="c-a0a fz-12">{{ userInfo.invita_num }}</view>
						</view>
					</view>
					<view  class="view flex-row align-center justify-between p-l-20 p-r-20">
						<view class="c-0D fz-12">团队人数</view>
						<view class="fz-12 c-a0a">{{ userInfo.team_num }}</view>
					</view>
			</view>

			<view style="width:calc(100vw - 150rpx);border-radius: 20px;" class="mauto bg-fff p-t-20 p-b-20 p-l-20 p-r-20 mypack-list m-b-12">
                <view  class="view flex-row align-center justify-between m-b-40" @click="bindPhone">
                    <view class="view flex-row align-center">
                        <image src="/static/img1/wallet_icon_changename_drfault.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">绑定手机</view>
                    </view>
                    <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"  v-if="userInfo.is_mobile == 0" />
					<view v-else class="fz-12 c-fff c-0D">{{ userInfo.mobile | $hiddenString(3,4) }}</view>
                </view>
                <view  class="view flex-row align-center justify-between" @click="toCertification">
                    <view class="view flex-row align-center">
                        <image src="/static/img1/wallet_icon_changename_drfault.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">实名认证</view>
                    </view>
                    <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15" v-if="userInfo.is_card == 0"/>
					<view v-else class="fz-12 c-fff c-0D">已认证</view>
                </view>
            </view>



            <view style="width:calc(100vw - 150rpx);border-radius: 20px;" class="mauto bg-fff p-t-20 p-b-20 p-l-20 p-r-20 mypack-list">
                <view class="view flex-row align-center justify-between m-b-40">
                    <view @click="toChangeWalletName" class="view flex-row align-center">
                        <image src="/static/img1/wallet_icon_changename_drfault.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">钱包名称</view>
                    </view>

                    <view @click="toChangeWalletName" class="view flex-row aling-center">
                        <view class="fz-12 c_a1a">{{Object.keys(defaultWallet).length > 0 ? defaultWallet.name : '未创建钱包'}}</view>
                        <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
                    </view>
					<uni-popup ref="popup3" type="center" :mask-click="false">
						<view class="popupCenters">
							<view class="center_title">
								<text>请输入新的钱包名称</text>
							</view>
							<view class="center_content">
								<uni-easyinput type="text" v-model="newName" :placeholder="'请输入新的钱包名称'"></uni-easyinput>
							</view>
							<!-- <text class="center_txt"> *用户名不得超过20个字符</text> -->
							<view class="center_button">
								<view class="btn1" @click="cancel1">{{'取消'}}</view>
								<view class="btn2" @click="changeWalletName">{{'保存'}}</view>
							</view>
						</view>
					</uni-popup>
                </view>

                <view @click="toChangePwd" class="view flex-row align-center justify-between m-b-40">
                    <view class="view flex-row align-center">
                        <image src="/static/img1/my_icon_change Password_default.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">修改密码</view>
                    </view>
                    <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
					<uni-popup ref="popup2" type="center" :mask-click="false"><view  class="popupsCenter">
						<view class="center_title"  v-model="changePswData.changePswModel"
						:closable="false"
						:mask-closable="false"
						:title="'修改密码'"
						>{{'修改密码'}}</view>
							<view class="center_text m-l-10 m-r-10">
								<!-- @input="oldPaw($event,'oldPsw')"  @input="newPsw($event,'newPsw')"  @input="againNewPsw($event,'againNewPsw')"-->
								<uni-easyinput type="password" v-model="changePswData.oldPsw" :placeholder="'请输入密码'"></uni-easyinput>
								<uni-easyinput class="m-t-20" type="password" v-model="changePswData.newPsw" :placeholder="'请输入新密码'"></uni-easyinput>
								<uni-easyinput class="m-t-20" type="password" v-model="changePswData.againNewPsw" :placeholder="'再次确认新密码'"></uni-easyinput>
							</view>
							<view class="center_button">
								<view class="btn1" @click="cancel">{{'取消'}}</view>
								<view class="btn2" @click="preservation">{{'保存'}}</view>
							</view>
						</view>
					</uni-popup>
                </view>
				
                <view @click="toPrivateKeyTip(2)" class="view flex-row align-center justify-between m-b-40">
                    <view class="view flex-row align-center">
                        <image src="/static/img1/my_icon_export private key_default.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">导出私钥</view>
                    </view>
                    <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
                </view>
                <view v-if="defaultWallet.type=='mnemonic'" @click="toPrivateKeyTip(1)" class="view flex-row align-center justify-between">
                    <view class="view flex-row align-center">
                        <image src="/static/img1/my_icon_address book_default.png" class="m-r-5 w20 h20"/>
                        <view class="fz-12 c-fff c-0D">导出助记词</view>
                    </view>
                    <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
                </view>
            </view>

           <view style="width:calc(100vw - 150rpx);border-radius: 20px;" class="mauto bg-fff m-t-20 p-t-20 p-b-20 p-l-20 p-r-20 mypack-list m-b-12">
               <view class="view flex-row align-center justify-between m-b-40" @click="downloadLink()">
                   <view class="view flex-row align-center">
                       <image src="/static/img1/my_icon_renew_default.png" class="w15 h15 m-r-5"/>
                       <view class="fz-12 c-0D">复制下载链接</view>
                   </view>
                   <image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
               </view>
				<view  class="view flex-row align-center justify-between m-b-40" @click="checkVersion()">
					<view class="view flex-row align-center">
						<image src="/static/img1/my_icon_renew_default.png" class="w15 h15 m-r-5"/>
						<view class="fz-12 c-0D">检查更新</view>
					</view>
					<view class="view flex-row align-center">
						<view class="c_a1a fz-12 m-r-5">当前版本：{{version}}</view>
						<image src="/static/img1/mywallet_icon_more_default.png" class="w15 h15"/>
					</view>
				</view>
           </view>
			<uni-popup ref="popup_version" type="center" :mask-click="false">
				<view  class="popupCenter">
				<view class="center_title">{{'检查更新'}}</view>
				<view class="center_content">{{'最新版本为'+this.versionNew+'是否前往下载更新?'}}</view>
				<view class="center_button">
					<view class="btn1" @click="cancel_version">{{'取消'}}</view>
					<view class="btn2" @click="openLink()">{{'前往更新'}}</view>
				</view>
				</view>
			</uni-popup>

        </view>
    </view>
	
</template>

<script>
import {mapState, mapGetters} from 'vuex';
import navbar from '@/components/uni-nav-bar'
import uniIcons from '@/components/uni-icons/uni-icons.vue'
import  uniEasyinput from '@/components/uni-easyinput/uni-easyinput'
import elButton from '@/components/el-button/el-button'
import noData from '@/components/noData/noData'
import sibList from '@/components/sib-list/sib-list.vue'
import { getUserInfoData,bindSuperior } from '@/apis/user'
import {registerNFTAddress} from "@/apis/pool.js"
import uniPopup from '@/components/uni-popup/uni-popup.vue'
import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
import {getVersion} from '@/apis/wallet.js';
import { aboutWallet } from "@/common/businessCommon";

export default {
    components: {
      navbar,
      uniEasyinput,
      uniIcons,
	  uniPopup,
	  uniPopupMessage,
	  uniPopupDialog,
	},
    data() {
        return {
			formData: {}, // 绑定手机
			userInfo: {}, // 用户信息
			at:'',
			version: "1.0.2", //todo 后续从配置中读取 
			versionNew:"",
			versionNewLink:"",
			superiorCode: "",
			newName:"",
			changePswData: {
			    changePswModel: false,
			    oldPsw: '',
			    oldPsw_pswLog_noBlank:'密码不能为空，请重新输入' ,
			    oldPsw_pswLog_noBlank_Flag: 'none',
			
			    newPsw: '',
			    newPsw_pswLog_noBlank:'密码不能为空，请重新输入',
			    newPsw_pswLog_noBlank_Flag: 'none',
			    pswLog_pattern:'密码由5-20位数字字母组成，请重新输入',
			    pswLog_pattern_Flag: 'none',
			
			    againNewPsw: '',
			    againNewPsw_pswLog_noBlank:'密码不能为空，请重新输入',
			    againNewPsw_pswLog_noBlank_Flag: 'none',
			    pswLog_equality:'两次输入密码不相等，请重新输入',
			    pswLog_equality_Flag: 'none'
			},
        }
    },
	computed: {
		...mapState(['defaultWallet']),
	    ...mapGetters(['hasDefaultWallet']),
	},
	onShow() {
	  this.getUserInfo()
	},
    methods: {
		toCertification() {
			if(!this.userInfo){
				uni.showToast({
					title:'数据读取中.',
					icon: 'none',
					duration: 2000
				})
			}else if(this.userInfo.is_card == 0){
				uni.navigateTo({
					url: '/pages/my/certification/certification'
				})
			}else{
				uni.showToast({
					title:'您已经绑定身份信息，如果信息检查不正确后续会驳回实名',
					icon: 'none',
					duration: 2000
				})
			}
		},
		bindPhone() {
			if(!this.userInfo){
				uni.showToast({
					title:'数据读取中',
					icon: 'none',
					duration: 2000
				})
			}else if(this.userInfo.is_mobile == 0){
				uni.navigateTo({
					url: '/pages/my/binding_mobile_phone_number/binding_mobile_phone_number'
				})
			}else{
				uni.showToast({
					title:'您已经绑定手机',
					icon: 'none',
					duration: 2000
				})
			}
		},
		async getUserInfo() {
			//这里再次登记可以避免大量的问题
			await registerNFTAddress()
			const res = await getUserInfoData()
			this.userInfo = res.data
		},
		checkVersion(){
			getVersion().then(back => {
				let res = back.data
				if(!res){
					res = back
				}
				console.log('检测结果',res.url);
				console.log(res);
				if(res.version == this.version){
					uni.showToast({
						title:'当前已是最新版本：'+ res.version,
						icon: 'none',
						duration: 2000
					})
				}else{
					this.versionNew = res.version;
					this.versionNewLink = res.url;
					this.$refs.popup_version.open()
				}
			});
		},
		downloadLink(){
			getVersion().then(back => {
				let res = back.data
				uni.setClipboardData({
				    data: res.downloadText,
				    success: function () {
				      uni.showToast({
				        title: '复制下载链接成功!',
				        icon: 'none'
				      })
				    }
				});
			});			
		},
		goto(url) {
			console.log('goto',url);
		    uni.navigateTo({
		      url: url
		    });
		},
		notOpen(){
			this.$showToast('该列表尚未开放');
		},
		yqmcode(){
			if(this.userInfo.yqmcode){
				uni.setClipboardData({
				    data: this.userInfo.yqmcode,
				    success: function () {
				      uni.showToast({
				        title: '复制邀请码成功!',
				        icon: 'none'
				      })
				    }
				});
			}else{
				uni.showToast({
					title:'绑定手机后即可生成邀请码',
					icon: 'none',
					duration: 2000
				})
			}
		},
		bindCode(){
			if(this.userInfo.is_superior == 1){
				uni.showToast({
					title:'已绑定邀请',
					icon: 'none',
					duration: 2000
				})
			}else{
				this.$refs.popup_bind.open()
			}
		},
		closeBind(){
			this.$refs.popup_bind.close()
		},
		async sendBind(){
			if(this.superiorCode){
				let bind = await bindSuperior({'project_yqmcode':this.superiorCode});
				console.log('bind',bind);
			}
		},
		mobileNumber(number){
			return number.substr(0,3) + "****" + number.substr(7)
		},
		//检查更新 取消
		cancel_version(){
			this.$refs.popup_version.close()
		},
		openLink(href){
			plus.runtime.openURL(this.versionNewLink);
		},
		//删除钱包
		toDeleteWallet() {
			this.$refs.popup1.open()
		},
		//已备份删除钱包
		deleteWalletAndSetDefault(){
			this.$refs.popup1.close()
			console.log('this.defaultWallet',this.defaultWallet,this.walletList);
			aboutWallet.deleteWalletAndSetDefault(this.defaultWallet.address);
			/*uni.showToast({
				title: '删除将和导出同步开放',
				icon: 'none',
				duration: 2000
			});*/
		},
		toPrivateKeyTip(type) {
			this.$refs.popup1.close()
			console.log("type",type)
			/*uni.showToast({
				title: '导出将于3月11开放',
				icon: 'none',
				duration: 2000
			});*/
			//return
			uni.navigateTo({ url: `/pages/wallet/mnemonic/output?type=${type}`})
		},
		
		//修改钱包名称
		toChangeWalletName(){
			console.log("修改钱包")
			console.log(this.defaultWallet.address)
			console.log('this.defaultWallet',this.defaultWallet);
			console.log('this.walletList',this.walletList);
			this.$refs.popup3.open()
		},
		//修改钱包名称取消
		cancel1(){
			this.$refs.popup3.close()
		},
		//修改钱包名称确认
		changeWalletName(){
			console.log("名称",this.newName)
			aboutWallet.changeWalletName(this.defaultWallet.address,this.newName);
			uni.showToast({
				title: '修改钱包名称成功',
				icon: 'none',
				duration: 2000
			});
			this.newName=''
			this.$refs.popup3.close()
		},
		
		// 修改密码
		toChangePwd(){
			console.log(this.changePswData.oldPsw,this.changePswData.newPsw,this.changePswData.againNewPsw)
			this.changePswData.oldPsw=''
			this.changePswData.newPsw=''
			this.changePswData.againNewPsw=''
			this.$refs.popup2.open()
			this.changePswModel = true;
		},
		//修改密码 取消
		cancel(){
			this.$refs.popup2.close()
		},
		// 修改密码
		preservation() {
			console.log('123123');
			let self = this;
		
			// console.log(self.validPsw())
			// console.log(this.defaultWallet.address)
			// console.log(self.changePswData.oldPsw)
			if(!aboutWallet.checkPassword(this.defaultWallet.address,self.changePswData.oldPsw)){
				uni.showToast({
				title:'旧密码错误!',
				icon: 'none',
				duration: 2000
				})
				this.changePswData.oldPsw=''
				console.log('校验旧密码');
				return;
			}
			console.log('333333');
		    if(self.changePswData.oldPsw  ===  self.changePswData.newPsw){
				uni.showToast({
				title:'旧密码与新密码相同!',
				icon: 'none',
				duration: 2000
				})
				console.log('校验旧密码与新密码');
				return;
			}
		
			 if(self.changePswData.newPsw==self.changePswData.againNewPsw){
				uni.showToast({
				title:'修改密码成功!',
				icon: 'none',
				duration: 2000
				})
				this.$refs.popup2.close()
				aboutWallet.changePassword(this.defaultWallet.address,self.changePswData.oldPsw,self.changePswData.newPsw)
		
		    }else{
				uni.showToast({
				title:'修改密码失败，请重新修改!',
				icon: 'none',
				duration: 2000
			});
				this.changePswData.oldPsw=''
				this.changePswData.newPsw=''
				this.changePswData.againNewPsw=''
		    }
		},
		oldPaw(e,flag){
			switch(flag){
				case 'oldPsw':{
					setTimeout(()=>{
						this.changePswData.oldPsw = e.target.value
						console.log("旧密码111",this.changePswData.oldPsw)
					},0)
					break;
				}
			}
		},
		newPsw(a,flag){
			switch(flag){
				case 'newPsw':{
					setTimeout(()=>{
						this.changePswData.newPsw = a.target.value
						console.log("新密码111",this.changePswData.newPsw)
					},0)
					break;
				}
			}
		},
		againNewPsw(b,flag){
			switch(flag){
				case 'againNewPsw':{
					setTimeout(()=>{
						this.changePswData.againNewPsw = b.target.value
						console.log("0新密码111",this.changePswData.againNewPsw)
					},0)
					break;
				}
			}
		},
    }
    
};
</script>

<style lang="scss" scoped>
.bar1-title {
    color:#3C3C3C;
    font-size: 32rpx;
}

page {
    background: #F6F6F6;
}
.left-bar-item {width: 100%;height:68px;display: flex;align-items: center;justify-content: center;}
.left-bar-item .image{
    width:30px;
    height: 30px;
}
.left-bar-item.active {
    background: #F6F6F6;
}
.btn-c {
    display: block;
    margin-top:56rpx;
    width: 100%;
    height: 96rpx;
    line-height: 78rpx;
    font-size: 40rpx;
}
.b-b-1 {border-bottom: 1px solid #EDEDED;}
.popupCenter,.popupsCenter,.popupCenters{
		position: fixed;
		top: 34%;
		left:10%;
		right:50%;
		width: 600rpx;
		margin: 0 auto;
		// height: 360rpx;
		border-radius: 20rpx;
		color: #333;
		background: #ffffff;
		display: flex;
		flex-direction: column;
		align-items: center;
		.center_title{
			width: 100%;
			text-align: center;
			margin: 20rpx auto;
			font-size: 36rpx;
			color: #333333;
			font-weight:700;
		}
		.center_content{
			width: 80%;
			text-align: center;
			margin: 20rpx auto;
			margin-bottom: 34rpx;
			// font-size: 38rpx;
		}
		.center_button{
			width: 98%;
			height: 100rpx;
			margin: 0 auto;
			margin-top: 10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #ffffff;
			// border-top: #dddddd;
			button{
				background: #ffffff;

				width: 48%;
				font-size: 34rpx;
				// border:#FFFFFF solid 1rpx;
			}
			.btn1{
				height: 90rpx;
				text-align: center;
				line-height: 90rpx;
				background: #ffffff;
				width: 100%;
				border-top:#ddd solid 1rpx;
				// border-right:#ddd solid 1rpx;
				border-bottom-left-radius: 20rpx;
			}
			.btn2{
				overflow: hidden;
				height: 90rpx;
				text-align: center;
				line-height: 90rpx;
				background: #ffffff;
				width: 100%;
				border-top:#ddd solid 1rpx;
				border-left:#ddd solid 1rpx;
				border-bottom-right-radius: 20rpx;
				color: #634DFF;
			}
		}

	}
	.popupsCenter{
		width: 600rpx;
		// height: 690rpx;
		top: 20%;
		left:10%;
		right:50%;
		.center_title{
			width: 100%;
			text-align: center;
			margin: 0 auto;
			margin-top: 30rpx;
			margin-bottom: 14rpx;
			font-size: 38rpx;
			color: #333333;
		}
		.center_text{
			width: 100%;
			// height: 70%;
			margin: 60rpx auto;
			display: flex;
			flex-direction: column;
			justify-content: flex-start;
			align-items: center;
			input{
				width: 80%;
				margin: 0 auto;
				font-size: 30rpx;
				color: #AAAAAA;
				height: 70rpx;
				border-bottom: 2rpx solid #ddd;
				margin-bottom: 30rpx;
			}
			.uni-easyinput{
				width: 90%;
			}
			.text-right{
				text-align: right;
				width: 80%;
				font-size: 26rpx;
				color: #5D5FFC;
				margin-top: -24rpx;
			}
		}
		.tips{
			width: 80%;
			color: #848484;
		}
		.input-frame{
			display: flex;
			justify-content: space-between;
			padding-bottom: 20rpx;
			border-bottom: 2rpx solid #aaa;
			.input-input{
				flex: 1;
				font-size: 30rpx;
				padding-right: 14rpx;
			}
			.input-btn{
				font-size: 30rpx;
				color: #5D5FFC;
			}
		}
	}
	// .popupCenters{
	// 	// height: 290rpx;
	// }
.mypack-list .view:last-child{
	margin-bottom: 0px;
}
.fix-bottom {width: 100vw;height: 100px;background: #fff;bottom: 0;left: 0;padding-top:32rpx;position: fixed;}

</style>
